﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LampServiceTools
{
    public class SocketCommand
    {
        public SocketCommand()
        {
            this.Message = null;
            this.Type = CommandType.Null;
        }

        public SocketCommand(byte[] msg)
        {
            //The first four bytes are for the Command
            this.Type = (CommandType)BitConverter.ToInt32(msg, 0);

            //The next four store the length of the message
            int msgLen = BitConverter.ToInt32(msg, 4);

            //This checks for a null message field
            if (msgLen > 0)
                this.Message = Encoding.UTF8.GetString(msg, 8, msgLen);
            else
                this.Message = null;
        }

        public byte[] ToByte()
        {
            List<byte> result = new List<byte>();

            //First four are for the Command
            result.AddRange(BitConverter.GetBytes((int)Type));

            //Length of the message
            if (Message != null)
                result.AddRange(BitConverter.GetBytes(Message.Length));
            else
                result.AddRange(BitConverter.GetBytes(0));

            //And, lastly we add the message text to our array of bytes
            if (Message != null)
                result.AddRange(Encoding.UTF8.GetBytes(Message));

            return result.ToArray();
        }

        public string Message { get; set; }
        public CommandType Type { get; set; }
    }

    public enum CommandType { GetUSB, Switcher, Arduino, Null }
}
